package com.gexiaobao.pigeon.ui.fragment.setting

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.EditAddressParams
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentModifyAddressBinding
import com.gexiaobao.pigeon.viewmodel.PersonalInfoViewModel
import com.lljjcoder.Interface.OnCityItemClickListener
import com.lljjcoder.bean.CityBean
import com.lljjcoder.bean.DistrictBean
import com.lljjcoder.bean.ProvinceBean
import com.lljjcoder.citywheel.CityConfig
import com.lljjcoder.style.citypickerview.CityPickerView
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 16:38
 * @Describe    : 修改地址
 */
class FragmentModifyAddress : BaseFragment<PersonalInfoViewModel, FragmentModifyAddressBinding>() {

    private val mPicker = CityPickerView()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose("修改地址") {
            nav().navigateUp()
        }
        initCityData()
    }

    override fun initData() {
        super.initData()
        val mAddress = arguments?.getString("address")
        val detailAddress = arguments?.getString("addressDetail")
        mViewModel.modifyAddress.set(mAddress)
        mViewModel.modifyDetailAddress.set(detailAddress)
    }

    private fun initCityData() {
        val cityConfig = CityConfig.Builder().build()
        cityConfig.title = "选择地区"
        cityConfig.titleTextSize = 20
        cityConfig.confirmTextSize = 18
        cityConfig.cancelTextSize = 18
        cityConfig.titleTextSize = 18
        cityConfig.visibleItems = 8
        cityConfig.defaultProvinceName = "山东省"//默认显示的省份
        cityConfig.defaultCityName = "青岛市"
        cityConfig.customItemLayout = R.layout.item_city
        cityConfig.customItemTextViewId = R.id.item_city_name_tv
        mPicker.init(context)
        mPicker.setConfig(cityConfig)

        mPicker.setOnCityItemClickListener(object : OnCityItemClickListener() {
            override fun onSelected(
                province: ProvinceBean?,
                city: CityBean?,
                district: DistrictBean?
            ) {
                super.onSelected(province, city, district)
                mViewModel.modifyAddress.set(province.toString() + " - " + city + " - " + district)
            }
        })
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.mineAddPigeonHomeAddressChoice,
            mDatabind.tvModifyAddressChoose,
            mDatabind.tvUpdateModifyAddressCommit
        ) {
            when (it) {
                mDatabind.tvModifyAddressChoose, mDatabind.mineAddPigeonHomeAddressChoice -> {
                    mPicker.showCityPicker()
                }

                mDatabind.tvUpdateModifyAddressCommit -> {
                    when {
                        mViewModel.modifyAddress.get()
                            .isEmpty() -> showMessage("请完善所在地区和详细地址")

                        mViewModel.modifyDetailAddress.get()
                            .isEmpty() -> showMessage("请完善所在地区和详细地址")

                        else -> {
                            val body = EditAddressParams(
                                address = mViewModel.modifyAddress.get(),
                                detailAddress = mViewModel.modifyDetailAddress.get()
                            )
                            mViewModel.editAddress(body)
                        }
                    }
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.editAddressResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

}